package com.cldz.service;

import com.cldz.common.dto.req.DevChannelDto;
import com.cldz.common.dto.req.ValuePropertyDto;
import com.cldz.common.dto.res.DeviceChannelResDto;
import com.cldz.dao.DevChannelMapper;
import com.cldz.entity.DeviceChannel;
import com.cldz.utils.BeanCopyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ Author     ：lixiao.
 * @ Date       ：Created in 13:21 2018/12/18
 * @ Description：
 * @ Modified By：
 */
@Service("devChannelService")
public class DevChannelService {

    @Autowired
    private DevChannelMapper devChannelMapper;


    public List<DeviceChannelResDto> getRegulatChannel(Integer devID){
        List<DeviceChannel> regulatChannel = devChannelMapper.getRegulatChannel(devID);
        return copyListBean(regulatChannel);
    }
    public List<DeviceChannelResDto> getControlChannel(Integer devID) {
        List<DeviceChannel> regulatChannel = devChannelMapper.getControlChannel(devID);
        return copyListBean(regulatChannel);
    }

    public List<ValuePropertyDto> getValueMeanListWithDevID(DevChannelDto devChannelDto) {
        return devChannelMapper.getValueMeanListWithDevID(devChannelDto);
    }

    public List<ValuePropertyDto> getValueMeanListWithDevType(DevChannelDto devChannelDto) {
        return devChannelMapper.getValueMeanListWithDevType(devChannelDto);
    }

    public List<DeviceChannelResDto> getDevChannels(DevChannelDto devChannelDto) {
        List<DeviceChannel> list = devChannelMapper.getDevChannels(devChannelDto);
        return copyListBean(list);
    }
    public Integer getDevChannelsAmount(DevChannelDto devChannelDto) {
        return devChannelMapper.getDevChannelsAmount(devChannelDto);
    }

    public List<DeviceChannelResDto> getDevChannelsWithTypeID(DevChannelDto devChannelDto) {
        List<DeviceChannel> list = devChannelMapper.getDevChannelsWithTypeID(devChannelDto);
        return copyListBean(list);
    }

    public Integer getDevChannelsAmountWithTypeID(DevChannelDto devChannelDto){

        return devChannelMapper.getDevChannelsAmountWithTypeID(devChannelDto);
    }

    public DevChannelDto getSpecifyChannel(DevChannelDto devChannelDto) {
        DeviceChannel deviceChannel = devChannelMapper.getSpecifyChannel(devChannelDto);
        DevChannelDto dto = new DevChannelDto();
        if(deviceChannel != null){
            BeanCopyUtil.copyPropertiesIgnoreNull(deviceChannel, dto);
        }else {
            return null;
        }
        return dto;
    }

    public List<DeviceChannelResDto> getBaseChannels(DevChannelDto devChannelDto){
        List<DeviceChannel> list = devChannelMapper.getBaseChannels(devChannelDto);
        return copyListBean(list);
    }

    public Integer getBaseChannelsAmount(DevChannelDto devChannelDto){
        return devChannelMapper.getBaseChannelsAmount(devChannelDto);
    }



    private List<DeviceChannelResDto> copyListBean(List<DeviceChannel> devListByTypeID) {
        List<DeviceChannelResDto> list = new ArrayList<>();
        for (DeviceChannel device : devListByTypeID) {
            DeviceChannelResDto dto = new DeviceChannelResDto();
            BeanCopyUtil.copyPropertiesIgnoreNull(device, dto);
//            BeanUtils.copyProperties(device, dto);
            list.add(dto);
        }
        return list;
    }



}
